
package com.probiz.estore.customer.service.impl;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.model.customer.Customer;
import com.probiz.estore.common.model.customer.Feedback;
import com.probiz.estore.common.model.customer.ShopPoint;
import com.probiz.estore.common.model.customer.ShopPointHistory;
import com.probiz.estore.common.model.order.SalesOrder;
import com.probiz.estore.common.model.product.ProductReview;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.core.util.I18nUtil;
import com.probiz.estore.customer.dao.ShopPointDao;
import com.probiz.estore.customer.dao.ShopPointHistoryDao;
import com.probiz.estore.customer.service.ShopPointManager;

/**
 * ShopPoint Business Delegate (Proxy) implementation to handle communication
 * between web and persistence layer. Implementation of ShopPointManager
 * interface. Developer introduced interfaces should be declared here. Won't get
 * overwritten.
 */
public class ShopPointManagerImpl extends GenericManagerImpl<ShopPoint> implements ShopPointManager {

	private ShopPointDao shopPointDao;
	private ShopPointHistoryDao shopPointHistoryDao;
	
	public void setShopPointDao(ShopPointDao shopPointDao) {
		this.shopPointDao = shopPointDao;
	}	

	public void setShopPointHistoryDao(ShopPointHistoryDao shopPointHistoryDao) {
		this.shopPointHistoryDao = shopPointHistoryDao;
	}

	@Override
	protected void initManager() {
		this.dao = shopPointDao;
	}

	@Override
	protected void preDelete(ShopPoint entity) {
		
	}

	@Override
	protected void preSave(ShopPoint entity) {
		
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ShopPointManager#updateShopPoint(com.probiz.estore.common.model.customer.Customer, java.lang.Integer, java.lang.Short, java.lang.Object)
	 */
	public boolean updateShopPoint(Customer customer, Integer point,Short type,Object entity){
		return updateShopPoint(customer, point,type, entity, null);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ShopPointManager#updateShopPoint(com.probiz.estore.common.model.customer.Customer, java.lang.Integer, java.lang.Short, java.lang.Object, java.lang.String)
	 */
	public boolean updateShopPoint(Customer customer, Integer point,Short type,Object entity,String description){
		ShopPoint shopPoint = customer.getShopPoint();
		if( (shopPoint.getTotal()+point) < 0 || point == 0){
			return false;
		}
		//现有积分满足减扣需要才执行。
		
		//----start:更新积分------
		if(ShopPoint.TYPE_PAY_RETURN != type){
			shopPoint.setTotal(shopPoint.getTotal()+ point);
			if (point > 0) {
				shopPoint.setGainedTotal(shopPoint.getGainedTotal() + point);
			} else {
				shopPoint.setUsedTotal(shopPoint.getUsedTotal() +  (-point));
			}
		}else{
			//特别地：返还积分逻辑不一样
			shopPoint.setTotal(shopPoint.getTotal()+ point);
			shopPoint.setUsedTotal(shopPoint.getUsedTotal() -  point);
		}
		save(shopPoint);
		//----end:  更新积分------
		
		//----start:增加历史------
		ShopPointHistory history = new ShopPointHistory();
		history.setCustomerId(customer.getAppuserId());
		history.setAmount(point);
		history.setShopPointType(type);
		
		String message = null;
		switch(type){
			case ShopPoint.TYPE_CUSTOMERREGISTER:
				message = I18nUtil.getInstance().getMessage("shopPointHistory.desc.customerregister",new Object[]{point});
				break;
			case ShopPoint.TYPE_FEEDBACK:
				history.setEntityId(((Feedback)entity).getFeedbackId());
				message = I18nUtil.getInstance().getMessage("shopPointHistory.desc.feedback",new Object[]{((Feedback)entity).getMessage(),point});
				break;
			case ShopPoint.TYPE_REVIEW:
				history.setEntityId(((ProductReview)entity).getProductReviewId());
				message = I18nUtil.getInstance().getMessage("shopPointHistory.desc.review",new Object[]{((ProductReview)entity).getProduct().getProductName(),((ProductReview)entity).getProduct().getProductCode(),((ProductReview)entity).getSubject(),point});
				break;
			case ShopPoint.TYPE_BUY:
				history.setEntityId(((SalesOrder)entity).getSalesOrderId());
				message = I18nUtil.getInstance().getMessage("shopPointHistory.desc.buy",new Object[]{((SalesOrder)entity).getOrderNo(),point});
				break;
			case ShopPoint.TYPE_PAY:
				history.setEntityId(((SalesOrder)entity).getSalesOrderId());
				message = I18nUtil.getInstance().getMessage("shopPointHistory.desc.pay",new Object[]{((SalesOrder)entity).getOrderNo(),-point});
				break;	
			case ShopPoint.TYPE_PAY_RETURN:
				history.setEntityId(((SalesOrder)entity).getSalesOrderId());
				message = I18nUtil.getInstance().getMessage("shopPointHistory.desc.payReturn",new Object[]{((SalesOrder)entity).getOrderNo(),point});
				break;	
			case ShopPoint.TYPE_MANUAL:
				message = I18nUtil.getInstance().getMessage("shopPointHistory.desc.manual",new Object[]{point});
				break;
			default:
				break;
		}
		if(StringUtils.isNotBlank(description)){
			message = description;
		}
		history.setDescription(message);
		shopPointHistoryDao.save(history);
		//----end:  增加历史------
		
		return true;
	}
}
